package com.jade.bss.service.push.impl;

import com.jade.bss.service.push.*;
import com.jade.framework.base.util.Parameters;
import org.apache.commons.lang3.StringUtils;

/**
 * 各个平台独立的Push Service接口，例如信鸽的平台。
 *
 * @author Daisy
 * @version 1.0 15-9-13 下午3:27
 */
public class PlatfromDepPushService
        extends AbstractPushService
{
    private PushService pushService;

    public void setPushService(PushService pushService)
    {
        this.pushService = pushService;
    }

    public PushResult pushNotification(String appCode, String platform, String audienceType, String[] audiences,
                                       boolean isAnd, String title, String notification, Parameters serviceParams,
                                       Parameters pushParams)
            throws PushException
    {
        if (StringUtils.equalsIgnoreCase(platform, PushConstants.PLATFORM_ANDROID) || StringUtils.equalsIgnoreCase(
                platform, PushConstants.PLATFORM_IOS)) {
            return pushService.sendPush(appCode + "_" + platform, audienceType, audiences, isAnd, title, notification,
                    serviceParams, pushParams);
        }
        else {
            PushResult result1 = pushService.pushNotification(appCode + "_" + PushConstants.PLATFORM_ANDROID, audienceType,
                    audiences,
                    isAnd, title, notification,
                    serviceParams, pushParams);
            PushResult result2 = pushService.pushNotification(appCode + "_" + PushConstants.PLATFORM_IOS, audienceType,
                    audiences, isAnd,
                    title, notification,
                    serviceParams, pushParams);
            if (result2.isOK()) {
                return result2;
            }
            else if (result1.isOK()) {
                return result1;
            }
            else {
                return result2;
            }
        }
    }

    public PushResult pushMessage(String appCode, String platform, String audienceType, String[] audiences,
                                  boolean isAnd, String title, String notification, Parameters serviceParams,
                                  Parameters pushParams)
            throws PushException
    {
        if (StringUtils.equalsIgnoreCase(platform, PushConstants.PLATFORM_ANDROID) || StringUtils.equalsIgnoreCase(
                platform, PushConstants.PLATFORM_IOS)) {
            return pushService.sendPush(appCode + "_" + platform, audienceType, audiences, isAnd, title, notification,
                    serviceParams, pushParams);
        }
        else {
            PushResult result1 = pushService.pushMessage(appCode + "_" + PushConstants.PLATFORM_ANDROID, audienceType,
                    audiences,
                    isAnd, title, notification,
                    serviceParams, pushParams);
            PushResult result2 = pushService.pushMessage(appCode + "_" + PushConstants.PLATFORM_IOS, audienceType,
                    audiences, isAnd,
                    title, notification,
                    serviceParams, pushParams);
            if (result2.isOK()) {
                return result2;
            }
            else if (result1.isOK()) {
                return result1;
            }
            else {
                return result2;
            }
        }
    }

    public ReceivedResult getReport(String appCode, long msgId)
            throws PushException
    {
        throw new UnsupportedOperationException("Unsupported operation");
    }
}
